package com.example.demo.util;

import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.impl.Base64Codec;

import java.util.Date;

/**
 * @Author: huanggaotao
 * @Description:
 * @Date: Created in 15:10 2021/4/25
 * @Modified By:
 */
public class CeshiJWT {

    public static void main(String[] args) {

        //创建JwtBuilder对象
        JwtBuilder jwtBuilder = Jwts.builder()
                //签名，第一个参数是算法，第二个参数是盐
                .signWith(SignatureAlgorithm.HS256, "hgt1")
                .claim("sub", "1234567890");//自定义claims

        //获取jwt的Token
        String token = jwtBuilder.compact();
        System.out.println(token);
        System.out.println("--------------------------------------");
        //分割
        String []strs = token.split("\\.");
        String header = Base64Codec.BASE64.decodeToString(strs[0]);
        String payload = Base64Codec.BASE64.decodeToString(strs[1]);
        //第三部分解析出来一定会乱码
        String sign = Base64Codec.BASE64.decodeToString(strs[2]);
        System.out.println("头部：" + header);
        System.out.println("负载：" + payload);
        System.out.println("签名：" + sign);



    }
}
